/*     */ package xin.petstore.framework.cas.session.redis;
/*     */ 
/*     */ import java.io.Serializable;
/*     */ import java.util.Collection;
/*     */ import java.util.HashSet;
/*     */ import java.util.Set;
/*     */ import org.apache.shiro.session.Session;
/*     */ import org.apache.shiro.session.UnknownSessionException;
/*     */ import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
/*     */ import org.springframework.data.redis.serializer.RedisSerializer;
/*     */ 
/*     */ public class RedisSessionDAO extends AbstractSessionDAO
/*     */ {
/*  26 */   private static Logger logger = LoggerFactory.getLogger(RedisSessionDAO.class);
/*     */ 
/*  28 */   private RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
/*     */   private RedisManager redisManager;
/*  38 */   private String keyPrefix = "shiro_redis_session:";
/*     */ 
/*     */   public void update(Session session) throws UnknownSessionException
/*     */   {
/*  42 */     saveSession(session);
/*     */   }
/*     */ 
/*     */   private void saveSession(Session session)
/*     */     throws UnknownSessionException
/*     */   {
/*  51 */     if ((session == null) || (session.getId() == null)) {
/*  52 */       logger.error("session or session id is null");
/*  53 */       return;
/*     */     }
/*  55 */     byte[] key = getByteKey(session.getId());
/*  56 */     byte[] value = this.serializer.serialize(session);
/*  57 */     session.setTimeout(this.redisManager.getExpire() * 1000);
/*  58 */     this.redisManager.set(key, value, this.redisManager.getExpire());
/*     */   }
/*     */ 
/*     */   public void delete(Session session)
/*     */   {
/*  63 */     if ((session == null) || (session.getId() == null)) {
/*  64 */       logger.error("session or session id is null");
/*  65 */       return;
/*     */     }
/*  67 */     this.redisManager.del(getByteKey(session.getId()));
/*     */   }
/*     */ 
/*     */   public Collection<Session> getActiveSessions()
/*     */   {
/*  73 */     Set sessions = new HashSet();
/*  74 */     Set keys = this.redisManager.keys(this.keyPrefix + "*");
/*  75 */     if ((keys != null) && (keys.size() > 0)) {
/*  76 */       for (byte[] key : keys) {
/*  77 */         Session s = (Session)this.serializer.deserialize(this.redisManager.get(key));
/*  78 */         sessions.add(s);
/*     */       }
/*     */     }
/*  81 */     return sessions;
/*     */   }
/*     */ 
/*     */   protected Serializable doCreate(Session session)
/*     */   {
/*  86 */     Serializable sessionId = generateSessionId(session);
/*  87 */     assignSessionId(session, sessionId);
/*  88 */     saveSession(session);
/*  89 */     return sessionId;
/*     */   }
/*     */ 
/*     */   protected Session doReadSession(Serializable sessionId)
/*     */   {
/*  94 */     if (sessionId == null) {
/*  95 */       logger.error("session id is null");
/*  96 */       return null;
/*     */     }
/*  98 */     Session s = null;
/*     */     try {
/* 100 */       s = (Session)this.serializer.deserialize(this.redisManager.get(getByteKey(sessionId)));
/*     */     } catch (Exception e) {
/* 102 */       logger.error("doReadSession error:\n", e);
/*     */     }
/* 104 */     return s;
/*     */   }
/*     */ 
/*     */   private byte[] getByteKey(Serializable sessionId)
/*     */   {
/* 113 */     String preKey = this.keyPrefix + sessionId;
/* 114 */     return preKey.getBytes();
/*     */   }
/*     */ 
/*     */   public void setSerializer(RedisSerializer<Object> serializer) {
/* 118 */     this.serializer = serializer;
/*     */   }
/*     */ 
/*     */   public RedisManager getRedisManager() {
/* 122 */     return this.redisManager;
/*     */   }
/*     */ 
/*     */   public void setRedisManager(RedisManager redisManager) {
/* 126 */     this.redisManager = redisManager;
/*     */ 
/* 131 */     this.redisManager.init();
/*     */   }
/*     */ 
/*     */   public String getKeyPrefix()
/*     */   {
/* 140 */     return this.keyPrefix;
/*     */   }
/*     */ 
/*     */   public void setKeyPrefix(String keyPrefix)
/*     */   {
/* 149 */     this.keyPrefix = keyPrefix;
/*     */   }
/*     */ }

/* Location:           C:\java代码反编译器\core-1.1.4.jar
 * Qualified Name:     com.eloancn.framework.cas.session.redis.RedisSessionDAO
 * JD-Core Version:    0.6.0
 */